package com.nuoniu.sibanyun.controller.po;


import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.po.VPoPurchaseOrderMain;
import com.nuoniu.sibanyun.service.po.IVPoPurchaseOrderMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.nuoniu.sibanyun.common.BaseController;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 采购订单管理
 * @author 小啊刚
 * @date 2020-11-16
 */
@RestController
@RequestMapping("/po/purchaseOrderMain")
@Api(value="采购订单管理", tags={"采购订单管理"})
public class VPoPurchaseOrderMainController extends BaseController {

    @Autowired
    private IVPoPurchaseOrderMainService orderMainService;

    @PostMapping("save")
    @ApiOperation("保存采购订单")
    public HttpResult save(@RequestBody VPoPurchaseOrderMain orderMain) {
        if(orderMain == null) {
            throw new NuoNiuException("数据不能为空");
        }
        if(orderMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        orderMainService.saveOrder(orderMain);
        return HttpResult.ok();
    }

    @PostMapping("saveOrderForVendor")
    @ApiOperation("保存单据（根据供应商 分单）")
    public HttpResult saveOrderForVendor(@RequestBody VPoPurchaseOrderMain orderMain) throws InterruptedException {
        if(orderMain == null) {
            throw new NuoNiuException("数据不能为空");
        }
        if(orderMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        orderMainService.saveOrderForVendor(orderMain);
        return HttpResult.ok();
    }

    @GetMapping("getDetail")
    @ApiOperation("获取采购订单")
    public HttpResult getDetail(VPoPurchaseOrderMain orderMain) {
        return HttpResult.ok(orderMainService.getById(orderMain.getId()));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除采购订单")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public HttpResult delete(Long id) {
        orderMainService.deleteById(id);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        orderMainService.submit(id, status);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        orderMainService.examine(id,status);
        return HttpResult.ok();
    }

    /**
     * 获取单据编码
     * @author 小啊刚
     * @date 2020-11-27
     */
    @GetMapping("getCode")
    @ApiOperation("获取单据编码")
    public HttpResult getCode() {
        return HttpResult.ok(orderMainService.getCode(UserThreadLocal.get().getCompanyId()));
    }

    /**
     * 关闭/打开
     */
    @GetMapping("closed")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:关闭,1:打开")
    })
    public HttpResult closed(Long id, Integer status) {
        orderMainService.closed(id, status);
        return HttpResult.ok();
    }
}
